package com.ssm.driver.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ssm.common.utils.R;
import com.ssm.driver.entity.vo.OrderDto;
import com.ssm.driver.entity.vo.SoOrderDto;
import com.ssm.driver.entity.vo.SodetailDto;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ssm.common.utils.PageUtils;
import com.ssm.common.utils.Query;

import com.ssm.driver.dao.SsmSjDao;
import com.ssm.driver.entity.SsmSjEntity;
import com.ssm.driver.service.SsmSjService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import static com.ssm.common.utils.Constant.LOGIN_CODE;


@Service("ssmSjService")
@AllArgsConstructor
@Log4j2
public class SsmSjServiceImpl extends ServiceImpl<SsmSjDao, SsmSjEntity> implements SsmSjService {
    //redis操作
    @Resource
    private SsmSjDao ssmSjDao;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SsmSjEntity> page = this.page(
                new Query<SsmSjEntity>().getPage(params),
                new QueryWrapper<SsmSjEntity>().eq("sj_state","1")
                        .eq("is_delete","1").or()
                        .eq("sj_state",0).eq("is_delete","1")
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils findPage(Map<String, Object> params) {
        IPage<SsmSjEntity> page = this.page(
                new Query<SsmSjEntity>().getPage(params), new QueryWrapper<SsmSjEntity>().eq("sj_state", "2").eq("is_delete","1")
        );
        return new PageUtils(page);
    }

    @Override
    public PageUtils findone(Map<String, Object> params) {
        String condition= (String) params.get("key");
        IPage<SsmSjEntity> page = this.page(
                new Query<SsmSjEntity>().getPage(params),
                new QueryWrapper<SsmSjEntity>()
                        .eq("sj_state","1")
                        .eq("is_delete","1")
                        .like(StringUtils.isNotBlank(condition),"sj_phone",condition).or()
                        .like(StringUtils.isNotBlank(condition), "sj_name",condition)
        );
        return new PageUtils(page);
    }
    @Override
    public PageUtils findone1(Map<String, Object> params) {
        String condition= (String) params.get("key");
        IPage<SsmSjEntity> page = this.page(
                new Query<SsmSjEntity>().getPage(params),
                new QueryWrapper<SsmSjEntity>()
                        .eq("sj_state","2")
                        .eq("is_delete","1")
                        .like(StringUtils.isNotBlank(condition),"sj_phone",condition).or()
                        .like(StringUtils.isNotBlank(condition), "sj_name",condition)
        );

        return new PageUtils(page);
    }

    /**
     * 冻结
     * @param sjIds
     * @return
     */
    @Override
    public Boolean freeze(Integer[] sjIds) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        int i=0;
        for (Integer sjId: sjIds) {
            updateWrapper.eq("sj_id",sjId);
            updateWrapper.set("sj_state",0);
             i += baseMapper.update(null, updateWrapper);
        }
        return i>0;
    }

    /**
     * 解冻
     * @param sjIds
     * @return
     */
    @Override
    public Boolean thaw(Integer[] sjIds) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        int i=0;
        for (Integer sjId: sjIds) {
            updateWrapper.eq("sj_id",sjId);
            updateWrapper.set("sj_state",1);
            i += baseMapper.update(null, updateWrapper);
        }
        return i>0;
    }

    /**
     * 发送验证码
     * @param phone
     * @return
     */
    @Override
    public R sendcode(String phone) {

        String code = RandomUtil.randomNumbers(6);
        stringRedisTemplate.opsForValue().set(LOGIN_CODE+phone,code,2L, TimeUnit.MINUTES);
        log.debug("发送验证码："+code);
        System.out.println("发送验证码："+code);
        return R.ok();
    }

    /**
     * 根据手机号查找司机
     * @param userphomne
     * @return
     */
    @Override
    public SsmSjEntity finduser(String userphomne) {
        QueryWrapper<SsmSjEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sj_phone",userphomne);
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 获取供货订单
     * @param sjId
     * @return
     */
    @Override
    public ArrayList<OrderDto> getOrder(String sjId) {
        ArrayList<OrderDto> orderlist = ssmSjDao.getOrder(sjId);

        return orderlist;
    }

    /**
     * 获取销售订单
     * @param sjId
     * @return
     */
    @Override
    public ArrayList<SoOrderDto> getsoOrder(String sjId) {
        ArrayList<SoOrderDto> soOrderList = ssmSjDao.getSoOrder(sjId);
        return soOrderList;
    }

    @Override
    public ArrayList<SodetailDto> getDetails(String id) {

        ArrayList<SodetailDto> detailList = ssmSjDao.getDetail(id);

        return detailList;
    }

}